package com.weixin.base.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;

import com.weixin.base.dao.MessageDao;
import com.weixin.common.BaseSpringJdbcDao;
import com.weixin.entity.TbAcceptMessage;
import com.weixin.entity.TbArea;
import com.weixin.entity.TbWxMessage;
import com.weixin.entity.TbWxUser;

public class MessageDaoImpl extends BaseSpringJdbcDao<TbWxUser, Integer>
		implements MessageDao {

	public Class getEntityClass() {
		return TbWxUser.class;
	}

	public void saveAcceptMessage(TbAcceptMessage tbAcceptMessage) {
		String sql = "INSERT INTO TbAcceptMessage "
				+ "(sToUserName,sFromUserName,dCreateTime,sMsgType,sContent,sMsgId,sPicUrl,sMediaId,sFormat,"
				+ "sThumbMediaId,sLocation_X,sLocation_Y,sScale,sLabel,sTitle,sDescription,sUrl)"
				+ " VALUES (:sToUserName,:sFromUserName,:dCreateTime,:sMsgType,:sContent,:sMsgId,:sPicUrl,:sMediaId,:sFormat,"
				+ ":sThumbMediaId,:sLocation_X,:sLocation_Y,:sScale,:sLabel,:sTitle,:sDescription,:sUrl)";
		getNamedParameterJdbcTemplate().update(sql,
				new BeanPropertySqlParameterSource(tbAcceptMessage));
	}

	public String selectToken(int userId) {
		String sToken = "";
		String sql = "SELECT top 1 sToken FROM tbwxuser where nUserId="
				+ userId;
		try {
			sToken = getSimpleJdbcTemplate().queryForObject(sql, String.class);
		} catch (DataAccessException e) {
			sToken = "";
			// e.printStackTrace();
		}
		return sToken;
	}

	/**
	 * 查询图文消息
	 * 
	 * @param userid
	 * @param nReplyType消息回复类型（1.关注回复
	 *            2自动回复 3自定义菜单回复 4关键字回复）
	 * @param ntype消息类型（1.文本2图片3语音4视频5音乐6图文）
	 * @return
	 */
	public List<Map<String, Object>> selectMessages(int userid, int nReplyType,
			int ntype) {

		StringBuffer sbf = new StringBuffer();

		sbf
				.append("SELECT sTitle,sDescription,sPicUrl,sWxUserUrl FROM tbwxmessage with (nolock) where 1=1 ");
		sbf.append(" AND nUserId=").append(userid).append(" AND ntype=")
				.append(ntype).append(" AND nReplyType=").append(nReplyType)
				.append(" AND nIsValid=1 ");
		sbf.append(" order by nOrder ");

		return getSimpleJdbcTemplate().queryForList(sbf.toString());
	}

	/**
	 * 查询图文消息
	 * 
	 * @param userid
	 * @param nReplyType消息回复类型（1.关注回复
	 *            2自动回复 3自定义菜单回复 4关键字回复）
	 * @param ntype消息类型（1.文本2图片3语音4视频5音乐6图文）
	 * @param nkey
	 *            key值
	 * @return
	 */
	public List<Map<String, Object>> selectMessagesByKey(int userid,
			int nReplyType, int ntype, int nkey) {

		StringBuffer sbf = new StringBuffer();

		sbf
				.append("SELECT sTitle,sDescription,sPicUrl,sWxUserUrl FROM tbwxmessage with (nolock) where 1=1 ");
		sbf.append(" AND nUserId=").append(userid).append(" AND ntype=")
				.append(ntype).append(" AND nReplyType=");
		sbf.append(nReplyType).append(" AND nKey=").append(nkey).append(
				" AND nIsValid=1 ");
		sbf.append(" order by nOrder ");

		return getSimpleJdbcTemplate().queryForList(sbf.toString());
	}
}
